/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import java.util.Properties;
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author JAVA
 */
public class MailSender {

    public MailSender() {
    }

    public static boolean send(String toAddress, String subject, boolean isHTMLFormat, String body) {
        MimeMultipart multipart = new MimeMultipart();
        Properties props = new Properties();
        // Nombre del host de correo, es smtp.gmail.com
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        // TLS si está disponible
        props.setProperty("mail.smtp.starttls.enable", "true");

        // Puerto de gmail para envio de correos
        props.setProperty("mail.smtp.port", "587");

        // Nombre del usuario
        props.setProperty("mail.smtp.user", "ejemplo@gmail.com");

        // Si requiere o no usuario y password para conectarse.
        props.setProperty("mail.smtp.auth", "true");
        
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("vccibernos@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO, toAddress);           
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            // BODY 
            MimeBodyPart mbp = new MimeBodyPart();
            if (isHTMLFormat) {
                mbp.setContent(body, "text/html");
            } else {
                mbp.setText(body);
            }
            multipart.addBodyPart(mbp);
            msg.setContent(multipart);


            Transport t=session.getTransport("smtp");
            t.connect("vccibernos@gmail.com","videoclub1234");
            t.sendMessage(msg,msg.getAllRecipients());
            t.close();
        } catch (Exception mex) {
            System.out.println(">> MailSender.send() error = " + mex);
            return false;
        }
        return true;
    }
    
    public static boolean sendAdjunto(String toAddress, String subject, boolean isHTMLFormat, String body) {
        MimeMultipart multipart = new MimeMultipart();
        Properties props = new Properties();
        // Nombre del host de correo, es smtp.gmail.com
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        // TLS si está disponible
        props.setProperty("mail.smtp.starttls.enable", "true");

        // Puerto de gmail para envio de correos
        props.setProperty("mail.smtp.port", "587");

        // Nombre del usuario
        props.setProperty("mail.smtp.user", "ejemplo@gmail.com");

        // Si requiere o no usuario y password para conectarse.
        props.setProperty("mail.smtp.auth", "true");
        
        Session session = Session.getDefaultInstance(props);
        session.setDebug(false);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("vccibernos@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO, toAddress);           
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            // BODY 
            MimeBodyPart mbp = new MimeBodyPart();
            if (isHTMLFormat) {
                mbp.setContent(body, "text/html");
            } else {
                mbp.setText(body);
            }
            BodyPart adjunto = new MimeBodyPart();
            adjunto.setDataHandler(new DataHandler(new FileDataSource("LogoFactory.JPEG")));
            adjunto.setFileName("LogoFactory.jpeg");
            multipart.addBodyPart(mbp);
            multipart.addBodyPart(adjunto);
            msg.setContent(multipart);


            Transport t=session.getTransport("smtp");
            t.connect("vccibernos@gmail.com","videoclub1234");
            t.sendMessage(msg,msg.getAllRecipients());
            t.close();
        } catch (Exception mex) {
            System.out.println(">> MailSender.send() error = " + mex);
            return false;
        }
        return true;
    }
}
